/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import entities.Article;
import entities.ArticleState;
import entities.Category;
import entities.UserProject;
import interfaces.ArticleInterface;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Thibault
 */
@ManagedBean
@SessionScoped
public class MyArticleBean implements Serializable {

    private static final long serialVersionUID = 1L;
    @EJB
    private ArticleInterface articleEJB;
    private Article selectedArticle;
    private String selectedState = "All";
    private Date endAuction;
    private List<String> selectedCategories = new ArrayList<String>();

    @PostConstruct
    public void init() {
    }

    public void articleSelected() {
        selectedCategories.clear();
        for (Category c : selectedArticle.getCategories()) {
            selectedCategories.add(c.toString());
        }
    }

    public void updateArticle() {
        articleEJB.updateArticle(selectedArticle);
        FacesMessage msg = new FacesMessage("Update done.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void updateArticleCategories() {
        selectedArticle.getCategories().clear();
        System.out.println("SelectedCateg : " + selectedCategories.size());
        for (String cateStr : selectedCategories) {
            selectedArticle.getCategories().add(Category.valueOf(cateStr));
        }
        this.updateArticle();
    }

    public void cancelArticle() {
        articleEJB.cancelArticle(selectedArticle);
        FacesMessage msg = new FacesMessage("Cancellation done.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void acceptAuction() {
        articleEJB.acceptAuction(selectedArticle);
        FacesMessage msg = new FacesMessage("The auction has been accepted.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void reSaleArticle() {
        articleEJB.reSaleArticle(selectedArticle, endAuction);
        FacesMessage msg = new FacesMessage("New auction created.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public ArticleState[] getArticleState() {
        return ArticleState.values();
    }

    public List<Article> getListArticle() {
        FacesContext fc = FacesContext.getCurrentInstance();
        UserProject loggedUser = ((HeaderBean) fc.getExternalContext().getSessionMap().get("headerBean")).getLoggedUser();
        try {
            ArticleState state = ArticleState.valueOf(selectedState);
            return articleEJB.getListArticleForUserIdAndState(loggedUser.getId(), state);
        } catch (IllegalArgumentException cce) {
            return articleEJB.getListArticleForUserId(loggedUser.getId());
        }
    }

    public Article getSelectedArticle() {
        return selectedArticle;
    }

    public void setSelectedArticle(Article selectedArticle) {
        this.selectedArticle = selectedArticle;
    }

    public String getSelectedState() {
        return selectedState;
    }

    public void setSelectedState(String selectedState) {
        this.selectedState = selectedState;
    }

    public Date getEndAuction() {
        return endAuction;
    }

    public void setEndAuction(Date endAuction) {
        this.endAuction = endAuction;
    }

    public List<String> getSelectedCategories() {
        return selectedCategories;
    }

    public void setSelectedCategories(List<String> selectedCategories) {
        this.selectedCategories = selectedCategories;
    }
}
